package com.abc.scholarship.controller;


import cn.hutool.core.bean.BeanUtil;
import com.abc.scholarship.common.utils.UserHolder;
import com.abc.scholarship.domain.dto.UserStudentDTO;
import com.abc.scholarship.domain.entity.User;
import com.abc.scholarship.domain.vo.resp.*;
import com.abc.scholarship.service.StudentService;
import com.abc.scholarship.service.UserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author jzw
 * @since 2024-07-02
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户相关接口")
public class UserController {

    @Resource
    private UserService userService;
    @Resource
    private StudentService studentService;

    /**获取全部用户
     *
     * @return
     */
    @GetMapping("/list")
    public ApiResult listAll(){
        return ApiResult.success(userService.list());
    }

//    /**
//     * 分页查询用户数据
//     * @param query
//     * @return
//     */
//    @GetMapping("/getList")
//    @ApiOperation("查询所有用户")
//    public ApiResult<PageDTO<UserStudentVO>> queryUserByPage(PageQuery query) {
//        return ApiResult.success(userService.queryUser(query));
//    }

    /**
     * 分页查询用户数据
     * @param userQueryVo
     * @return
     */
    @GetMapping("/getList")
    @ApiOperation("查询所有用户")
    public ApiResult queryUserByPage(UserQueryVo userQueryVo) {
        Page<UserStudentVO> page = new Page<>(userQueryVo.getPageNo(),userQueryVo.getPageSize());
        // 调用 Service 层的查询方法
        IPage<UserStudentVO> resultPage = userService.queryUser(page, userQueryVo);
        userService.queryUser(page,userQueryVo);
        return ApiResult.success(resultPage);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    @ApiOperation("根据Id查询用户")
    public ApiResult<UserStudentVO> selectOne(@PathVariable Long id) {
        return ApiResult.success(this.userService.getUserById(id));
    }

    /**
     * 通过主键查询单条数据
     *
     *
     * @return 单条数据
     */
    @GetMapping("/getUserInfo")
    @ApiOperation("学生信息获取(用户端)")
    public ApiResult<StudentResp> getUserInfo() {
        return ApiResult.success(this.studentService.getUserInfo(UserHolder.getUId()));
    }

    @GetMapping("/getTeacherInfo")
    @ApiOperation("老师信息获取(用户端)")
    public ApiResult<TeacherResp> getTeacherInfo() {
        return ApiResult.success(
                BeanUtil.toBean(this.userService.getById(UserHolder.getUId()), TeacherResp.class)
        );
    }

    /**
     * 新增数据
     *
     * @param userStudentDTO 实体对象
     * @return 新增结果
     */
    @PostMapping("/addUser")
    @ApiOperation("新增用户")
    public ApiResult addUser(@RequestBody UserStudentDTO userStudentDTO) {
        userService.addUser(userStudentDTO);
        return ApiResult.success("添加成功");
    }

    /**
     * 修改数据
     *
     * @param userStudentDTO 实体对象
     * @return 修改结果
     */

    @PutMapping("/updateUser")
    @ApiOperation("修改用户")
    public ApiResult update(@RequestBody UserStudentDTO userStudentDTO) {
        userService.updateUser(userStudentDTO);
        return ApiResult.success();

    }

    @GetMapping("/remove/{id}")
    @ApiOperation("删除用户")
    public ApiResult deleteById(@PathVariable Long id){
        User u = userService.getById(id);
        if(u.getStatus()==1) {
            u.setStatus(2);
        }else{
            u.setStatus(1);
        }
        userService.updateById(u);
        return ApiResult.success("修改成功");
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/deleteUser")
    @ApiOperation("根据IdList删除用户")
    public ApiResult delete(@RequestParam("idList") List<Long> idList) {
        return ApiResult.success(this.userService.removeByIds(idList));
    }


    @ApiOperation("导入学生数据")
    @PostMapping("/import")
    public ApiResult<String> importStu(MultipartFile multipartFile){
        userService.importStu(multipartFile);

        return ApiResult.success("导入成功");
    }


}

